package com.platform.common.utils;

import java.util.Collection;
import java.util.Map;

/**
 * String工具类
 *
 * @author 肖亮
 */
public class StringUtils {

	public static boolean isEmpty(Object str) {
		return str == null || str.toString().length() == 0;
	}

	public static boolean isNotBlank(String str) {
		return !StringUtils.isBlank(str);
	}

	/**
	 * str == null    	true< br/>
	 * str == "" 		true< br/>
	 * str == "   "		true< br/>
	 * str == "a  "		false< br/>
	 * str == "abc"		false< br/>
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((!Character.isWhitespace(str.charAt(i)))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 加一拼成字符串 如传入 10, 4 返回 1005
	 *
	 * @param parentId
	 *            parentId
	 * @param maxId
	 *            maxId
	 * @return String 加1
	 */
	public static String addOne(String parentId, String maxId) {
		int ten = 10;
		if (Constant.STR_ZERO.equals(parentId)) {
			parentId = "";
		}
		if (isNullOrEmpty(maxId)) { //10192_01
			return parentId + "01";
		}
		
		
		
		
		
		maxId = maxId.substring(maxId.length() - 2);
		
		//101 92
		
		int result = Integer.parseInt(maxId) + 1;
		if (result < ten) {
			return parentId + "0" + result;
		} else {
			return parentId + result + "";
		}
	}

	/**
	 * 判断对象或对象数组中每一个对象是否为空: 对象为null，字符序列长度为0，集合类、Map为empty
	 *
	 * @param obj
	 *            obj
	 * @return boolean
	 */
	public static boolean isNullOrEmpty(Object obj) {
		if (obj == null) {
			return true;
		}

		if (obj instanceof CharSequence) {
			return ((CharSequence) obj).length() == 0;
		}

		if (obj instanceof Collection) {
			return ((Collection) obj).isEmpty();
		}

		if (obj instanceof Map) {
			return ((Map) obj).isEmpty();
		}

		if (obj instanceof Object[]) {
			Object[] object = (Object[]) obj;
			if (object.length == 0) {
				return true;
			}
			boolean empty = true;
			for (Object anObject : object) {
				if (!isNullOrEmpty(anObject)) {
					empty = false;
					break;
				}
			}
			return empty;
		}
		return false;
	}

	/**
	 * 生成key
	 *
	 * @param prefix
	 *            前缀
	 * @param className
	 *            类名
	 * @param methodName
	 *            方法名
	 * @return prefix className.methodName
	 */
	public static String genKey(String prefix, String className, String methodName) {
		return prefix + "userId_" + ShiroUtils.getUserId() + "_" + className + "." + methodName;
	}
}
